home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / sound / sndplaydoublebuffer / _source / simpleapp_sound.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  10.1 KB  |  334 lines

  1. /*
  2.     File:        SimpleApp_Sound.c
  3.  
  4.     Contains:    Routines demonstrating how to play a sound with many of the same features as
  5.                 QuickTime.
  6.  
  7.     Written by: Mark Cookson    
  8.  
  9.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 8/31/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. #include "SimpleApp_Sound.h"
  25.  
  26. /*-----------------------------------------------------------------------*/
  27. pascal    void    MyScrollAction (ControlHandle control, short part)
  28. /*-----------------------------------------------------------------------*/
  29. {
  30.     long    position = GetControlValue (control);
  31.  
  32.     if (part) {
  33.         switch (part) {
  34.             case kControlUpButtonPart:
  35.                 position -= oneBuffer;
  36.                 if (CDStyle == true) {
  37.                     if (gPlayBackwards == true) {
  38.                         ASoundPlayBackwards (mySoundInfo, true);
  39.                     }
  40.                 }
  41.                 break;
  42.             case kControlDownButtonPart:
  43.                 position += oneBuffer;
  44.                 break;
  45.             case kControlPageUpPart:
  46.                 position -= tenBuffers;
  47.                 if (CDStyle == true) {
  48.                     if (gPlayBackwards == true) {
  49.                         ASoundPlayBackwards (mySoundInfo, true);
  50.                     }
  51.                 }
  52.                 break;
  53.             case kControlPageDownPart:
  54.                 position += tenBuffers;
  55.                 break;
  56.             default:
  57.                 DebugStr ("\pWhere did you click?");
  58.         }
  59.         SetControlValue (control, position);
  60.         ASoundSetCurBuffer (mySoundInfo, position);
  61.  
  62.         if (CDStyle == true) {
  63.             ASoundSetCurBuffer (mySoundInfo, position);
  64.         }
  65.     }
  66. }
  67.  
  68. /*-----------------------------------------------------------------------*/
  69. pascal    void    DoScroll (const ControlHandle control,const short pPart)
  70. /*-----------------------------------------------------------------------*/
  71. {
  72.     long    position    = nil;
  73.     Point    pt            = {nil, nil};
  74.     short    part        = pPart;
  75.     OSErr    theError    = noErr;
  76.     
  77.     if (part != nil) {
  78.         switch (part) {
  79.             case kControlIndicatorPart:
  80.                 if (CDStyle == false) {
  81.                     theError = ASoundPauseForAdjust (mySoundInfo);
  82.                 }
  83.                 GetMouse(&pt);
  84.                 part = TrackControl (control, pt, nil);
  85.                 position = GetControlValue (control);
  86.                 ASoundSetCurBuffer (mySoundInfo, position);
  87.                 break;
  88.             case kControlUpButtonPart:
  89.             case kControlDownButtonPart:
  90.             case kControlPageUpPart:
  91.             case kControlPageDownPart: {
  92.                 ControlActionUPP actionProc = NewControlActionProc (MyScrollAction);
  93.                 if (CDStyle == false) {
  94.                     theError = ASoundPauseForAdjust (mySoundInfo);
  95.                 }
  96.                 part = TrackControl (control, pt, actionProc);
  97.                 DisposeRoutineDescriptor (actionProc);
  98.                 break;
  99.             }
  100.         }
  101.     
  102.         if (CDStyle == false) {
  103.             position = GetControlValue (control);
  104.             ASoundSetCurBuffer (mySoundInfo, position);
  105.             theError = ASoundPauseForAdjust (mySoundInfo);
  106.         }
  107.     }
  108. }
  109.  
  110. /*-----------------------------------------------------------------------*/
  111.         void    MyIdleProc (EventRecord *evt)
  112. /*-----------------------------------------------------------------------*/
  113. {
  114. #ifndef __SC__
  115. #pragma unused (evt)
  116. #endif
  117.     long        curBuf        = nil;
  118.  
  119.     if (soundPlaying == true) {
  120.         curBuf = ASoundGetCurBuffer (mySoundInfo);
  121.         if (curBuf != nil) {
  122.             SetControlValue (scrollBar, curBuf);
  123.         }
  124.     }
  125.  
  126.     if (ASoundIsDone (mySoundInfo) == true) {
  127.         EnableControl (startButton);
  128.         DisableControl (stopButton);
  129.         DisableControl (resumeButton);
  130.         DisableControl (scrollBar);
  131.         SetControlValue (scrollBar, kScrollMinValue);
  132.         ASoundDonePlaying (mySoundInfo, kCloseFile + kFreeMem);
  133.         SAL_InstallIdleProc((SalEvent_ProcPtr)nil);
  134.     }
  135. }
  136.  
  137. /*-----------------------------------------------------------------------*/
  138. pascal OSErr    DoPlay (const ButtonItemRef pButtonItemRef,const long modifiers)
  139. /*-----------------------------------------------------------------------*/
  140. {
  141.     OSErr    theErr;
  142.  
  143. #pragma unused (pButtonItemRef,modifiers)
  144.  
  145.     theErr = ASoundGetFileToPlay (mySoundInfo);
  146.     if (theErr == noErr) {
  147.         theErr = ASoundStartPlaying (mySoundInfo, nil);
  148.         if (theErr == noErr) {
  149.             SAL_DisableMe();
  150.             EnableControl(resumeButton);
  151.             EnableControl(stopButton);
  152.             paused = false;
  153.             SAL_InstallIdleProc ((SalEvent_ProcPtr)MyIdleProc);
  154.             EnableControl (scrollBar);
  155.             SetControlMaximum (scrollBar, ASoundGetNumBuffers (mySoundInfo) - 1);
  156.         }
  157.     }
  158.  
  159.     return theErr;
  160. }
  161.  
  162. /*-----------------------------------------------------------------------*/
  163. pascal OSErr DoPause(const ButtonItemRef pButtonItemRef,const long modifiers)
  164. /*-----------------------------------------------------------------------*/
  165. {
  166.     Boolean            theError;
  167.  
  168. #pragma unused (pButtonItemRef,modifiers)
  169.  
  170.     theError = ASoundPause (mySoundInfo);
  171.  
  172.     if (paused == false) {
  173.         paused = true;
  174.         SAL_SetTitle ("\pResume Sound");
  175.     }
  176.     else {
  177.         paused = false;
  178.         SAL_SetTitle ("\pPause Sound");
  179.     }
  180.  
  181.     return theError;
  182. }
  183.  
  184. /*-----------------------------------------------------------------------*/
  185. pascal OSErr    DoStop(const ButtonItemRef pButtonItemRef,const long modifiers)
  186. /*-----------------------------------------------------------------------*/
  187. {
  188.     OSErr theError;
  189.  
  190. #pragma unused (pButtonItemRef,modifiers)
  191.  
  192.     theError = ASoundStop (mySoundInfo);
  193.  
  194.     SAL_DisableMe();
  195.     EnableControl(startButton);
  196.     DisableControl(resumeButton); 
  197.  
  198.     if (paused == true) {
  199.         SetControlTitle(resumeButton,"\pPause Sound");
  200.     }
  201.  
  202.     return theError;
  203. }
  204.  
  205. /*-----------------------------------------------------------------------*/
  206. pascal    short    DoPostGroupHit (const long  modifiers,
  207.                                 const ControlRef theControl,
  208.                                 const ButtonItemRef brh,
  209.                                 const short item)
  210. /*-----------------------------------------------------------------------*/
  211. {
  212. #pragma unused (modifiers)
  213. #pragma unused (theControl)
  214. #pragma unused (brh)
  215.  
  216.     switch (item) {
  217.         case qtStyle:
  218.             DisableControl (checkBox);
  219.             SetControlValue (checkBox, false);
  220.             CDStyle = false;
  221.             break;
  222.         case cdStyle:
  223.             EnableControl (checkBox);
  224.             SetControlValue (checkBox, gPlayBackwards);
  225.             CDStyle = true;
  226.             break;
  227.         default:
  228.             DebugStr ("\pDoPostGroupHit got an invalid control passed to it!");
  229.     }
  230.  
  231.     return nil;
  232. }
  233.  
  234. /*-----------------------------------------------------------------------*/
  235. pascal short DoPlayBackwards(const ButtonItemRef pButtonItemRef,const long modifiers)
  236. /*-----------------------------------------------------------------------*/
  237. {
  238. #pragma unused (pButtonItemRef,modifiers)
  239.  
  240.     gPlayBackwards = !gPlayBackwards;
  241.     SetControlValue (checkBox, gPlayBackwards);
  242.  
  243.     return nil;
  244. }
  245.  
  246. /*-----------------------------------------------------------------------*/
  247.         void    main (void)
  248. /*-----------------------------------------------------------------------*/
  249. {
  250.     Rect    r;
  251.     short    err;
  252.     Str255 names[] = {"\pQuickTime style",
  253.                       "\pCD style"};
  254.  
  255.     SAL_InitSimpleApp (2, kSAL_UseStandardMenu);          /* Simple App Sets up the Tool Box For us */
  256.     SAL_GetDocumentWindow(128,nil);                     /* Get our stored window */
  257.  
  258.     SAL_SetRectLocation (&r, 10, 2);                /* This Sets a rects anchor point */
  259.     SAL_SetRectDimensions (&r, 90, 20);                /* This Sets its size without changing it position */
  260.     err = SAL_InstallPushButton (1, gSAL_CurrentWindow, "\pPlay Sound", &r, (char)0,  DoPlay, nil);
  261.     startButton = gSAL_CurrentControl;
  262.  
  263.     SAL_SetRectLocation (&r, 110, 2);                /* This Sets a rects anchor point */
  264.     SAL_SetRectDimensions (&r, 100, 20);            /* This Sets its size without changing it position */
  265.     err = SAL_InstallPushButton (2, gSAL_CurrentWindow, "\pPause Sound", &r, (char)0,  DoPause, nil);
  266.     resumeButton  = gSAL_CurrentControl;
  267.     DisableControl (resumeButton);
  268.  
  269.     SAL_SetRectLocation (&r, 220, 2);                /* This Sets a rects anchor point */
  270.     SAL_SetRectDimensions (&r, 90, 20);                /* This Sets its size without changing it position */
  271.     err = SAL_InstallPushButton (3, gSAL_CurrentWindow, "\pStop Sound", &r, (char)0,  DoStop, nil);
  272.     stopButton = gSAL_CurrentControl;                /* lets remember the control handle */
  273.     DisableControl (stopButton);                    /* disable the control */
  274.  
  275.     SAL_SetRectDimensions (&r, 300, 16);
  276.     SAL_SetRectLocation (&r, 10, 30);
  277.     err = SAL_InstallScrollBar (4, gSAL_CurrentWindow, "\p", &r, (char)0, DoScroll, nil,nil);
  278.     scrollBar = gSAL_CurrentControl;        /* get the object control handle */
  279.     DisableControl (scrollBar);
  280.     SetControlMinimum (scrollBar, kScrollMinValue);
  281.     SetControlMaximum (scrollBar, kScrollMinValue);
  282.     SetControlValue (scrollBar, kScrollMinValue);
  283.  
  284.     SAL_SetRectLocation (&r, 20, 100);
  285.     err = SAL_InstallCheckBox(5, gSAL_CurrentWindow, "\pBackwards playing", &r, nil, false, DoPlayBackwards, nil);
  286.     checkBox = gSAL_CurrentControl;
  287.     DisableControl (checkBox);
  288.  
  289.     SAL_SetRectLocation (&r, 10, 60);
  290.     SAL_SetRectDimensions (&r, 130, 40);
  291.     err    = SAL_InstallRadioGroup (6,                /* Group ID */
  292.                             gSAL_CurrentWindow,    /* owner window */
  293.                             2,                    /* Number of items */
  294.                             "\pRadio Group",    /* Group Name */
  295.                             &names,                /* Array of button Names */
  296.                             &r,                 /* Group bounds */
  297.                             nil,                /* default item (zero based) */
  298.                             2,                     /* Horz spacing */
  299.                             2,                     /* vert spacing */
  300.                             16,                    /* Button Height */
  301.                             130,                /* Button widths */
  302.                             nil,                /* Pre Hit Proc */
  303.                             DoPostGroupHit,     /* Post Hit Proc */
  304.                             nil                    /* Group Update Proc */
  305.                             );
  306.  
  307.     SAL_GetPrintArea (gSAL_CurrentWindow,&r);            /* Set the print area top coordinate */
  308.     r.top = 22;                                  /* so that you don't scroll the button out of view */
  309.     SAL_SetPrintArea (gSAL_CurrentWindow,&r);
  310.     mySoundInfo = ASoundNew (&err);
  311.  
  312.     if (err == noErr)
  313.         SAL_Run ();                                 /* Let SimpleApp handle the rest */
  314. }
  315.  
  316. // utility functions since I already keep the control handle around
  317. /*-----------------------------------------------------------------------*/
  318.     void EnableControl (ControlRef cr)
  319. /*-----------------------------------------------------------------------*/
  320. {
  321.     if (cr != nil) {
  322.         HiliteControl(cr, nil);
  323.     }
  324. }
  325.  
  326. /*-----------------------------------------------------------------------*/
  327.     void DisableControl (ControlRef cr)
  328. /*-----------------------------------------------------------------------*/
  329. {
  330.     if (cr != nil) {
  331.         HiliteControl(cr, 255);
  332.     }
  333. }
  334.